package model;
import java.io.*;
import java.util.ArrayList;


/**
 * Class to handle the Data, read in a file and so on.
 * Creates a LinkedList to stor the nodes in it.
 * @author walonka
 * @version 1.0
 *
 */
public class DataHandler {
	private ArrayList<AIDataNode> nodes;
	public ArrayList<AIDataNode> getList(){
		return this.nodes;
	}
	
	/**
	 * Constructor that initializes the LinkedList.
	 */
	public DataHandler(){
		nodes = new ArrayList<AIDataNode>();
	}
	/**
	 * Method to read in the nodes from a given file
	 * @param filename filename of the dataSource.
	 */
	public void readFile(String filename){
		try {
			FileReader reader = new FileReader(filename);
			BufferedReader br = new BufferedReader(reader);
			br.readLine(); //Skip first line
			String line;
			while((line=br.readLine())!=null){
				String[] split = line.split(","); //split at every ,
				//DataNode(num_pregnant, plasma_conc, blood_pres, triceps, insulin, bMI, pedigree, age, klass)
				double[] tmpD = new double[split.length-1];
				for (int i=0; i<split.length-1 ;i++){
					tmpD[i]=Double.parseDouble(split[i]);
				}
				AIDataNode tmp = new AIDataNode(tmpD, split[split.length-1]);
				nodes.add(tmp);
			}
			br.close();
			reader.close();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Splitting up the Array into a subarray including the startpoint exluding the end
	 * @param start included start, first index included in the new arraylist
	 * @param end end, not included in the arraylist
	 * @return new Arraylist with specified data
	 */
	public ArrayList<AIDataNode> splitAt(int start, int end){
		ArrayList<AIDataNode> tmp = new ArrayList<AIDataNode>();
		for (int i=start; i<end; i++){
			tmp.add(nodes.get(i));
		}
		return tmp;		
	}
}
